<?php
namespace App\Admin\Controllers\Traits\WxInviteTraits;

use App\Admin\Renderable\UsersRender;
use App\Models\User\WxUser;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Intervention\Image\Facades\Image;

trait formUserInviteTempCodeBgTrait{
    public function index_userinvite_itc_bg(Content $content)
    {
        return $content->title('背景图配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_userinvite_itc_bg()->render()}</div>
HTML
            );
    }

    protected function form_userinvite_itc_bg(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">你当前想使用哪个，就把哪个顶到最上面</div>';
        }, '');
        $form->html(function () {
            return '<a style="color: #5b92e1;font-weight: bold;" target="_blank" href="'.admin_url('invite_benefit/action/itc_bg_config_test').'">生成效果图</a>';
        }, '');
        $form->table('app_user_invite_temp_code_bg','背景图', function (NestedForm $table) {
            $table->image('img', '图片')->url('files/uploads')->autoUpload()->uniqueName();
            $table->rate('x_rate', 'x轴偏移');
            $table->rate('y_rate', 'y轴偏移');
            $table->rate('c_rate', '推广码放缩')->default(1.0);
        })->default(Settings::get('app_user_invite_temp_code_bg', ''));


        $form->action('invite_benefit/action/itc_bg_config');
        return $form;
    }

    public function userinvite_itc_bg_test(Request $request){
        $temp_code_bg = Settings::get('app_user_invite_temp_code_bg', []);
        if(_empty_($temp_code_bg)){
            exit('<h1>您没有配置背景图，无法查看效果</h1>');
        }
        $the_bg = $temp_code_bg[0];

        $imageUrl = $the_bg['img'];
        $qrCodeUrl = 'https://img.mini.minisns.cn/2024/09/30/0/f733a4ce05d21085e19e4b867626377b.png';
        $xPercent = round(($the_bg['x_rate'] ?? 0)/100, 2); // 默认值为0.5
        $yPercent = round(($the_bg['y_rate'] ?? 0)/100, 2); // 默认值为0.5
        $scale = $the_bg['c_rate'] ?? 1; // 默认值为1

        // 加载主图像
        $image = Image::make($imageUrl);

        // 加载二维码图像
        $qrCode = Image::make($qrCodeUrl);

        // 调整二维码图像大小
        $qrCode->resize($qrCode->width() * $scale, $qrCode->height() * $scale);

        // 计算二维码图像在主图像中的位置
        $x = $image->width() * $xPercent - ($qrCode->width() / 2);
        $y = $image->height() * $yPercent - ($qrCode->height() / 2);

        // 将二维码图像粘贴到主图像上
        $image->insert($qrCode, 'top-left', intval($x), intval($y));

        // 输出图像到屏幕
        return $image->response('jpg');
    }

    public function userinvite_itc_bg(Request $request){

        Settings::set('app_user_invite_temp_code_bg', AdminUtils::_table_data($request->input('app_user_invite_temp_code_bg')), true);

        return $this->response()->success('成功！');
    }

}
